package com.hustzp.com.xichuangzhu.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVCloudQueryResult;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.CloudQueryCallback;
import com.avos.avoscloud.SaveCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.login.LoginActivity;
import com.hustzp.com.xichuangzhu.poetry.model.LikePost;
import com.hustzp.com.xichuangzhu.poetry.model.Works;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/28 0:06
 * @Description:一日一赏列表适配器
 */
public class LikesPostAdapter extends BaseAdapter {
    private Context context;
    private List<LikePost> likePosts;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat sdfDate = new SimpleDateFormat("MM月dd日");
    private OnPostClickListener onPostClickListener;

    public OnPostClickListener getOnPostClickListener() {
        return onPostClickListener;
    }

    public void setOnPostClickListener(OnPostClickListener onPostClickListener) {
        this.onPostClickListener = onPostClickListener;
    }

    public LikesPostAdapter(Context context, List<LikePost> likePosts) {
        this.context = context;
        this.likePosts = likePosts;
    }

    public interface OnPostClickListener {
        void onClick(Works works);
    }

    @Override
    public int getCount() {
        return likePosts.size();
    }

    @Override
    public LikePost getItem(int position) {
        return likePosts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context,
                    R.layout.fragment_day_item, null);
            holder.initView(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        bindViews(position, holder);
        return convertView;
    }

    private void bindViews(int position, ViewHolder holder) {
        final LikePost post = getItem(position);
//        ImageUtils.loadImage("", holder.imgAvator, R.mipmap.xcz_logo);
        holder.tvDate.setText(getSDay(post.getCreatedAt()));
        holder.tvQuote.setText(post.getTEXT());
        Works works = post.getWorks();
        if (works != null) {
            holder.tv_poetry_title.setText(works.getTitle());
            holder.tv_poetry_content.setText(works.getContent());
            holder.ll_poetry_layout.setTag(works);
        }
        holder.tv_comments_count.setText(context.getResources().getText(R.string.comment) + " " + post.getCOMMENTSCOUNT());

        if (post.isLiked()) {
            holder.tv_likes_count.setText(context.getResources().getText(R.string.one_day_rewar_prised) + " " + post.getLIKESCOUNT());
            holder.tv_likes_count.setTag(post.getLikeAVObj().getObjectId());
        } else {
            holder.tv_likes_count.setText("赞" + " " + post.getLIKESCOUNT());
            holder.tv_likes_count.setTag(null);
        }

        holder.tv_likes_count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TextView tv = (TextView) v;
                String tvCntStr = tv.getText().toString();
                final int tvCnt = Integer.valueOf(tvCntStr.split(" ")[1]);

                if (AVUser.getCurrentUser() == null) {
                    context.startActivity(new Intent(context, LoginActivity.class));
                    return;
                }

                if (v.getTag() == null) {
                    final AVObject avObj = new AVObject("LikePost");
                    avObj.put("user", AVUser.getCurrentUser());
                    avObj.put("post", post);
                    avObj.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(AVException e) {
                            if (e == null) {
                                tv.setText("已赞 " + (tvCnt + 1));
                                tv.setTag(avObj.getObjectId());
//                                Toast.makeText(context, "点赞成功", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } else {
                    AVQuery.doCloudQueryInBackground("delete from LikePost where objectId='" + v.getTag() + "'", new CloudQueryCallback<AVCloudQueryResult>() {
                        @Override
                        public void done(AVCloudQueryResult avCloudQueryResult, AVException e) {
                            if (e == null) {
                                tv.setText("赞 " + (tvCnt - 1));
                                tv.setTag(null);
//                                Toast.makeText(context, "取消点赞", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });
    }

    private String getSDay(Date date) {
        String result = "";
        int day = isYesterday(sdf.format(date));
        switch (day) {
            case 0:
                result = "今天";
                break;
            case -1:
                result = "昨天";
                break;
            default:
                result = sdfDate.format(date);
        }
        return result;
    }

    private int isYesterday(String date) {
        int day = 0;
        try {
            Date d1 = new Date();//当前时间
            Date d2 = sdf.parse(date);//传进的时间
            long cha = d2.getTime() - d1.getTime();
            day = new Long(cha / (1000 * 60 * 60 * 24)).intValue();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return day;

    }


    class ViewHolder {
        ImageView imgAvator;
        TextView tvDate;
        TextView tvQuote;
        TextView tv_poetry_title;
        TextView tv_poetry_content;
        TextView tv_comments_count;
        TextView tv_likes_count;
        LinearLayout ll_poetry_layout;

        public void initView(View view) {
            imgAvator = (ImageView) view.findViewById(R.id.imgAvatar);
            ll_poetry_layout = (LinearLayout) view.findViewById(R.id.ll_poetry_layout);
            tvDate = (TextView) view
                    .findViewById(R.id.tvDate);
            tvQuote = (TextView) view
                    .findViewById(R.id.tvQuote);
            tv_poetry_title = (TextView) view
                    .findViewById(R.id.tv_poetry_title);
            tv_poetry_content = (TextView) view
                    .findViewById(R.id.tv_poetry_content);
            tv_comments_count = (TextView) view
                    .findViewById(R.id.tv_comments_count);
            tv_likes_count = (TextView) view
                    .findViewById(R.id.tv_likes_count);


            ll_poetry_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Works work = (Works) v.getTag();
                    if (getOnPostClickListener() != null) {
                        getOnPostClickListener().onClick(work);
                    }
                }
            });
        }
    }
}
